/*
 * $Id: GenericRoller.java 20 2010-06-12 04:27:23Z dave.terhune $
 *
 * Copyright (C) 2009, 2010 David Terhune
 *
 * This file is part of DiceRoller.
 *
 * DiceRoller is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * DiceRoller is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with DiceRoller.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.dterhune.gaming.dice;

import android.app.Activity;
import android.view.View;
import android.widget.TextView;
import java.util.Random;

/** Rolls generic dice. */
@SuppressWarnings("serial") // Never actually serialized, so ignore warning.
public class GenericRoller extends Random implements View.OnClickListener
{
    /** Result view. */
    private TextView result;
    /** Sum view. */
    private TextView sum;
    /** Edit text with number of 4-sided dice to roll. */
    private TextView x4;
    /** Edit text with number of 6-sided dice to roll. */
    private TextView x6;
    /** Edit text with number of 8-sided dice to roll. */
    private TextView x8;
    /** Edit text with number of 10-sided dice to roll. */
    private TextView x10;
    /** Edit text with number of 12-sided dice to roll. */
    private TextView x12;
    /** Edit text with number of 20-sided dice to roll. */
    private TextView x20;
    /** Edit text with number of percentile dice to roll. */
    private TextView x100;
    
    /**
     * Creates a new GenericRoller.
     * @param activity The parent DiceRoller
     */
    public GenericRoller(Activity activity)
    {
        // Set the button click listener
        View b = activity.findViewById(R.id.roll_generic);
        b.setOnClickListener(this);

        // Cache the text fields
        result = (TextView)activity.findViewById(R.id.generic_result);
        sum = (TextView)activity.findViewById(R.id.generic_sum);
        x4 = (TextView)activity.findViewById(R.id.x4);
        x6 = (TextView)activity.findViewById(R.id.x6);
        x8 = (TextView)activity.findViewById(R.id.x8);
        x10 = (TextView)activity.findViewById(R.id.x10);
        x12 = (TextView)activity.findViewById(R.id.x12);
        x20 = (TextView)activity.findViewById(R.id.x20);
        x100 = (TextView)activity.findViewById(R.id.x100);
    }

    /**
     * Click handler.
     * @param v view that was clicked
     */
    public void onClick(View v)
    {
        // Get the number of each die to roll, and then roll them, accumulating
        // results and the sum as we go.
        int s = 0;
        StringBuilder sb = new StringBuilder();
        boolean previous = false;

        // Start with the d4
        int nd = parseField(x4);
        if(nd > 0)
        {
            s += add(nd, 4, sb);
            previous = true;
        }

        // Add in the d6
        nd = parseField(x6);
        if(nd > 0)
        {
            if(previous)
            {
                sb.append('\n');
            }
            s += add(nd, 6, sb);
            previous = true;
        }

        // Add in the d8
        nd = parseField(x8);
        if(nd > 0)
        {
            if(previous)
            {
                sb.append('\n');
            }
            s += add(nd, 8, sb);
            previous = true;
        }

        // add in the d10
        nd = parseField(x10);
        if(nd > 0)
        {
            if(previous)
            {
                sb.append('\n');
            }
            s += add(nd, 10, sb);
            previous = true;
        }

        // add in the d12
        nd = parseField(x12);
        if(nd > 0)
        {
            if(previous)
            {
                sb.append('\n');
            }
            s += add(nd, 12, sb);
            previous = true;
        }

        // add in the d20
        nd = parseField(x20);
        if(nd > 0)
        {
            if(previous)
            {
                sb.append('\n');
            }
            s += add(nd, 20, sb);
            previous = true;
        }

        // add in the d100
        nd = parseField(x100);
        if(nd > 0)
        {
            if(previous)
            {
                sb.append('\n');
            }
            s += add(nd, 100, sb);
            previous = true;
        }

        // Now put the results into the appropriate places
        result.setText(sb);
        sum.setText(Integer.toString(s));
    }

    /**
     * Add a series of dice rolls, accumulating the individual results.
     * @param n Number of dice to roll
     * @param s Number of sides the dice have
     * @param sb String builder to append the individual die rolls to
     * @return Sum of the dice rolled
     */
    private int add(int n, int s, StringBuilder sb)
    {
        int total = 0;
        for(int i = 0; i < n; i++)
        {
            if(i != 0)
            {
                // Not first item; add a comma
                sb.append(", ");
            }
            int v = nextInt(s) + 1;
            total += v;
            sb.append(v);
        }
        return total;
    }

    /**
     * Parse a numeric value from a text field.
     * @param view the text field to parse
     * @return the value in the field, or 0 if there is a parse error
     */
    private int parseField(TextView view)
    {
        int value = 0;
        try
        {
            value = Integer.parseInt(view.getText().toString());
        }
        catch(NumberFormatException ex)
        {
            value = 0;
        }
        return value;
    }
}
